package com.oyjd.business.listview.activity;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.oyjd.R;
import com.oyjd.business.listview.service.ListViewDemoService;
import com.oyjd.business.listview.vo.ListViewDemoVO;
import com.oyjd.framework.service.CallBack;
import com.oyjd.framework.ui.listview.ListViewEx;
import com.oyjd.framework.util.ImageHelp;

/**
 * ListViewEx使用例子
 * 
 * @author 欧建东
 * @email oyjd@163.com
 * 
 */
public class ListViewDemo extends Activity {
	private LayoutInflater flater;// 布局加载器

	private ListViewEx listview;

	private ListViewDemoService service = new ListViewDemoService();

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// 初始化布局加载器
		flater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		listview = new ListViewEx(this);
		listview.setPageSize(service.pageSize);
		listview.setBackgroundColor(Color.WHITE);
		setContentView(listview);

		listview.setOnListViewExListener(new ListViewEx.ListViewExListener() {

			@Override
			public void onRefreshClick() {
				service.getFirstData(new CallBack() {

					@SuppressWarnings("unchecked")
					@Override
					public void back(Object data) {
						List<ListViewDemoVO> list = (List<ListViewDemoVO>) data;
						List<View> views = listview.getViews();
						views.clear();
						for (ListViewDemoVO vo : list) {
							views.add(createItemView(vo));
						}
						listview.notifyRefreshFinished();
					}
				});

			}

			@Override
			public void onMoreClick() {
				service.getMoreData(new CallBack() {

					@SuppressWarnings("unchecked")
					@Override
					public void back(Object data) {
						List<ListViewDemoVO> list = (List<ListViewDemoVO>) data;
						List<View> views = listview.getViews();
						for (ListViewDemoVO vo : list) {
							views.add(createItemView(vo));
						}
						listview.notifyMoreFinished();

					}
				});
			}
		});

		// 首次加载
		service.getFirstData(new CallBack() {

			@SuppressWarnings("unchecked")
			@Override
			public void back(Object data) {
				List<ListViewDemoVO> list = (List<ListViewDemoVO>) data;
				List<View> views = listview.getViews();
				views.clear();
				for (ListViewDemoVO vo : list) {
					views.add(createItemView(vo));
				}
				listview.notifyRefreshFinished();
			}
		});
	}

	/**
	 * 根据数据创建视图
	 * 
	 * @param vo
	 * @return
	 */
	private View createItemView(ListViewDemoVO vo) {
		View view = flater.inflate(R.layout.list_item_demo, null);
		((TextView) view.findViewById(R.id.lv_tv_m1_author)).setText(vo.getName());
		((TextView) view.findViewById(R.id.lv_tv_m1_time)).setText(vo.getTime());
		((TextView) view.findViewById(R.id.lv_tv_m1_title)).setText(vo.getTitle());
		ImageView img = (ImageView) view.findViewById(R.id.lv_img_m1_pic);
		ImageHelp.doImg(img, vo.getPicUrl());
		view.setTag(vo);
		return view;
	}
}
